class Api::V1::UsersController < Api::V1::BaseController
  def show
    if @current_user.id == params[:id]
      @user = @current_user
      @is_self = true
    else
      @user = User.find params[:id]
      @is_self = false
    end
  end

  def update
    if params[:user][:password].presence
      if not @current_user.authenticate(params[:user][:password])
        render json: { result: false, message: '密码验证失败.' }, status: :ok and return
      end
      if not params[:user][:new_password] == params[:user][:confirm_password]
        render json: { result: false, message: '新密码两次输入不相同.' }, status: :ok and return
      end
      params[:user][:password] = params[:user][:new_password]
    end
    
    if @current_user.update user_params
      @user = @current_user
    else
      render json: { result: false, message: '更新错误.' }, status: :ok
    end
  end
end
